package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.*;
import com.qf.service.DtsMallService;
import com.qf.util.JacksonUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author luo
 */
@RestController
@RequestMapping("/admin")
@CrossOrigin
public class MallController {
    @Autowired
    private DtsMallService dtsMallService;
    /**
     * ###  商场管理
     *
     * #### 2.2.1. 行政区域
     *
     * ##### 2.2.1.1. 行政区域列表
     *
     * - 接口地址 : http://localhost:8083/admin/region/list?page=1&limit=20
     *
     * - 下行数据 :
     */
    @RequiresPermissionsDesc(menu = {"商场管理","行政区域"},button = "行政区域列表")
    @RequiresPermissions(value = "admin:region:list")
    @GetMapping("/region/list")
    public Object regionOfList(Integer page,Integer limit,String name,String code){
        IPage<DtsRegion> iPage=dtsMallService.findRegionList(page,limit,name,code);
        Map data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     *接口地址 : http://localhost:8083/admin/brand/list?page=1&limit=20&sort=add_time&order=desc
     */
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button = "品牌制造商查询")
    @RequiresPermissions(value = "admin:brand:list")
    @GetMapping("/brand/list")
    public Object brandOfList(Integer page,Integer limit,String sort,String order,String name,Integer id){
        IPage<DtsBrand> iPage=dtsMallService.findBrandList(page,limit,sort,order,name,id);
        Map data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     *接口地址 : 接口地址 :  http://localhost:8083/admin/brand/catAndAdmin
     */
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button = "品牌和管理员查询")
    @RequiresPermissions(value = "admin:brand:catAndAdmin")
    @GetMapping("/brand/catAndAdmin")
    public Object brandCatAndAdminList(){
        //查询管理员列表
        List<GenVo> adminList=dtsMallService.findAdminList();
        //查询category列表
        List<CatVo> categoryList=dtsMallService.findCategoryList();
        Map data = new HashMap<>();
        data.put("adminList",adminList);
        data.put("categoryList",categoryList);
        return ResponseUtil.ok(data);
    }

    /**
     *接口地址 : http://localhost:8083/admin/brand/create
     */
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button = "添加")
    @RequiresPermissions(value = "admin:brand:create")
    @PostMapping("/brand/create")
    public Object brandOfCreate(@RequestBody DtsBrand dtsBrand){
        dtsMallService.addBrand(dtsBrand);
        //查询品牌商数据
        return ResponseUtil.ok(dtsBrand);
    }

    /**
     * 接口地址 : http://localhost:8083/admin/brand/update
     */
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button = "修改")
    @RequiresPermissions(value = "admin:brand:update")
    @PostMapping("/brand/update")
    public Object brandOfUpdate(@RequestBody DtsBrand dtsBrand){
        dtsMallService.modifyBrand(dtsBrand);
        return ResponseUtil.ok(dtsBrand);
    }

    /**
     * 接口地址 : http://localhost:8083/admin/brand/delete
     */
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button = "删除")
    @RequiresPermissions(value = "admin:brand:delete")
    @PostMapping("/brand/delete")
    public Object brandOfDelete(@RequestBody DtsBrand dtsBrand){
        dtsMallService.removeBrand(dtsBrand);
        return ResponseUtil.ok();
    }

    /**
     * 接口地址 : http://localhost:8083/admin/category/list?page=1&limit=20&sort=add_time&order=desc
     */
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button = "类目查询")
    @RequiresPermissions(value = "admin:category:list")
    @GetMapping("/category/list")
    public Object categoryOfList(Integer page,Integer limit,String sort,String order,Integer id,String name){
        IPage<DtsCategory> iPage=dtsMallService.findCategoryListAll(page,limit,sort,order,id,name);
        Map data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     * 接口地址 : http://localhost:8083/admin/category/l1
     */
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button = "一级类目查询")
    @RequiresPermissions(value = "admin:category:l1")
    @GetMapping("/category/l1")
    public Object categoryOfL1(){
        return ResponseUtil.ok(dtsMallService.findCategoryByOne());
    }

    /**
     * 接口地址 : http://localhost:8083/admin/category/create
     */
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button = "添加")
    @RequiresPermissions(value = "admin:category:create")
    @PostMapping("/category/create")
    public Object categoryOfCreate(@RequestBody DtsCategory dtsCategory){
        dtsMallService.addCategory(dtsCategory);
        return ResponseUtil.ok(dtsCategory);
    }

    /**
     * 接口地址 : http://localhost:8083/admin/category/update
     */
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button = "编辑")
    @RequiresPermissions(value = "admin:category:update")
    @PostMapping("/category/update")
    public Object categoryOfUpdate(@RequestBody DtsCategory dtsCategory){
        dtsMallService.modifyCategory(dtsCategory);
        return ResponseUtil.ok();
    }

    /**
     * 接口地址 : http://localhost:8083/admin/category/delete
     */
    @RequiresPermissionsDesc(menu = {"商场管理","商品类目"},button = "删除")
    @RequiresPermissions(value = "admin:category:delete")
    @PostMapping("/category/delete")
    public Object categoryOfDelete(@RequestBody DtsCategory dtsCategory){
        dtsMallService.removeCategory(dtsCategory);
        return ResponseUtil.ok();
    }


    /**
     * 接口地址 : http://localhost:8083/admin/order/list?page=1&limit=20&sort=add_time&order=desc
     */
    @RequiresPermissionsDesc(menu = {"商场管理","订单管理"},button = "订单查询")
    @RequiresPermissions(value = "admin:order:list")
    @GetMapping("/order/list")
    public Object orderOfList(Integer page,Integer limit,String sort,String order,Integer userId,String orderSn,Integer[] orderStatusArray){
        IPage<DtsOrder> iPage=dtsMallService.findOrderList(page,limit,sort,order,userId,orderSn,orderStatusArray);
        Map data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     * 接口地址 : http://localhost:8083/admin/order/listShipChannel
     * */
    @RequiresPermissionsDesc(menu = {"商场管理","订单管理"},button = "快递查询")
    @RequiresPermissions(value = "admin:order:listShipChannel")
    @GetMapping("/order/listShipChannel")
    public Object orderOfListShipChannel(){
        List<ShipVo> shipChannelList = new ArrayList<>();
        ShipVo shipVo1 = new ShipVo();
        shipVo1.setValue("HTKY");
        shipVo1.setLabel("百世快递");
        ShipVo shipVo2 = new ShipVo();
        shipVo2.setValue("ZTO");
        shipVo2.setLabel("中通快递");
        ShipVo shipVo3 = new ShipVo();
        shipVo3.setValue("YTO");
        shipVo3.setLabel("圆通速递");
        ShipVo shipVo4 = new ShipVo();
        shipVo4.setValue("YD");
        shipVo4.setLabel("韵达速递");
        shipChannelList.add(shipVo1);
        shipChannelList.add(shipVo2);
        shipChannelList.add(shipVo3);
        shipChannelList.add(shipVo4);
        Map data = new HashMap<>();
        data.put("shipChannelList", shipChannelList);
        return ResponseUtil.ok(data);
    }

    /**
     * 接口地址 : http://localhost:8080/admin/order/ship
     * */
    @RequiresPermissionsDesc(menu = {"商场管理","订单管理"},button = "快递选择")
    @RequiresPermissions(value = "admin:order:ship")
    @PostMapping("/order/ship")
    public Object orderOfFetch(@RequestBody String body){
        Integer id = JacksonUtil.parseInteger(body, "orderId");
        String shipSn = JacksonUtil.parseString(body, "shipSn");
        String shipChannel = JacksonUtil.parseString(body, "shipChannel");
        dtsMallService.addShip(id,shipSn,shipChannel);
        return ResponseUtil.ok();
    }

    /**
     * 接口地址 : http://localhost:8083/admin/order/detail?id=98
     * */
    @RequiresPermissionsDesc(menu = {"商场管理","订单管理"},button = "订单详情")
    @RequiresPermissions(value = "admin:order:detail")
    @GetMapping("/order/detail")
    public Object orderOfDetail(Integer id){
        List<DtsOrderGoods> orderGoods=dtsMallService.findDetailListById(id);
        DtsUser user=dtsMallService.findUserOrderId(id);
        DtsOrder order=dtsMallService.findOrderListById(id);
        Map data = new HashMap<>();
        data.put("orderGoods", orderGoods);
        data.put("user", user);
        data.put("order", order);
        return ResponseUtil.ok(data);
    }

    /**
     * 接口地址 ：http://localhost:8080/admin/issue/list?page=1&limit=20&question=nuh&sort=add_time&order=desc
     */
    @RequiresPermissionsDesc(menu = {"商场管理","通用问题"},button = "查询")
    @RequiresPermissions(value = "admin:issue:list")
    @GetMapping("/issue/list")
    public Object issueOfList(Integer page,Integer limit,String sort,String order,String question){
        IPage<DtsIssue> dtsIssueIPage=dtsMallService.findQuestionList(page,limit,sort,order,question);
        Map data = new HashMap<>();
        data.put("total", dtsIssueIPage.getTotal());
        data.put("items", dtsIssueIPage.getRecords());
        return ResponseUtil.ok(data);
    }
    /**
     * 接口地址 ：http://localhost:8080/admin/issue/create
     */
    @RequiresPermissionsDesc(menu = {"商场管理","通用问题"},button = "添加")
    @RequiresPermissions(value = "admin:issue:create")
    @PostMapping("/issue/create")
    public Object issueOfCreate(@RequestBody DtsIssue dtsIssue){
        dtsMallService.addIssue(dtsIssue);
        return ResponseUtil.ok();
    }
    /**
     * 接口地址 ：http://localhost:8080/admin/issue/update
     */
    @RequiresPermissionsDesc(menu = {"商场管理","通用问题"},button = "编辑")
    @RequiresPermissions(value = "admin:issue:update")
    @PostMapping("/issue/update")
    public Object issueOfUpdate(@RequestBody DtsIssue dtsIssue){
        dtsMallService.modifyIssue(dtsIssue);
        return ResponseUtil.ok();
    }
    /**
     * 接口地址 ：http://localhost:8080/admin/issue/delete
     */
    @RequiresPermissionsDesc(menu = {"商场管理","通用问题"},button = "删除")
    @RequiresPermissions(value = "admin:issue:delete")
    @PostMapping("/issue/delete")
    public Object issueOfDelete(@RequestBody DtsIssue dtsIssue){
        dtsMallService.removeIssue(dtsIssue);
        return ResponseUtil.ok();
    }

    /**
     * 接口地址 ：http://localhost:8080/admin/keyword/list?page=1&limit=20&keyword=fsa&url=qqq&sort=add_time&order=desc
     */
    @RequiresPermissionsDesc(menu = {"商场管理","关键词"},button = "查询")
    @RequiresPermissions(value = "admin:keyword:list")
    @GetMapping("/keyword/list")
    public Object keywordOfList(Integer page,Integer limit,String sort,String order,String keyword,String url){
        IPage<DtsKeyword> iPage=dtsMallService.findKeywordList(page,limit,sort,order,keyword,url);
        Map data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return ResponseUtil.ok(data);
    }
    /**
     * 接口地址：http://localhost:8080/admin/keyword/create
     */
    @RequiresPermissionsDesc(menu = {"商场管理","关键词"},button = "添加")
    @RequiresPermissions(value = "admin:keyword:create")
    @PostMapping("/keyword/create")
    public Object keywordOfCreate(@RequestBody DtsKeyword dtsKeyword){
        dtsMallService.addKeyword(dtsKeyword);
        return ResponseUtil.ok();
    }
    /**
     * 接口地址：http://localhost:8080/admin/keyword/update
     */
    @RequiresPermissionsDesc(menu = {"商场管理","关键词"},button = "编辑")
    @RequiresPermissions(value = "admin:keyword:update")
    @PostMapping("/keyword/update")
    public Object keywordOfUpdate(@RequestBody DtsKeyword dtsKeyword){
        dtsMallService.modifyKeyword(dtsKeyword);
        return ResponseUtil.ok();
    }
    /**
     * 接口地址：http://localhost:8080/admin/keyword/delete
     */
    @RequiresPermissionsDesc(menu = {"商场管理","关键词"},button = "删除")
    @RequiresPermissions(value = "admin:keyword:delete")
    @PostMapping("/keyword/delete")
    public Object keywordOfDelete(@RequestBody DtsKeyword dtsKeyword){
        dtsMallService.removeKeyword(dtsKeyword);
        return ResponseUtil.ok();
    }

}
